#include"./include/mprpcapplication.h"
#include<iostream>
#include<unistd.h>
// #define test

MprpcConfig MprpcApplication::m_config;

void ShowArgsHelp(){
	 std::cout<< "format: command -i <configfile>"<<std::endl;
}


void MprpcApplication::Init(int argc,char** argv){
    if(argc<2){
		//没传入参数,答应帮助说明
		ShowArgsHelp();
		exit(EXIT_FAILURE);
    }

	int c = 0;
	std::string config_file;
	//表示参数选项里面要有一个i，i后面加：表示后面必须有一个参数
	while((c=getopt(argc,argv,"i:"))!=-1){
		switch(c){
			case 'i':
				config_file = optarg; //optarg为后面跟的参数
				break;
			case '?': //选项出现了不希望出现的选项
				// std::cout<<"我难道是在这？？"<<std::endl;
				// std::cout<<"invalid args"<<std::endl;
				ShowArgsHelp();
				exit(EXIT_FAILURE);
			case ':': // 出现了-i，但是没有带参数
				// std::cout<<"Need <configfile>"<<std::endl;
				ShowArgsHelp();
				exit(EXIT_FAILURE);
			default:
				break;
		}
	}

	//加载配置文件，配置文件里面的信息为
	/*
	rpcserver_ip = ?
	rpcserver_port = ?
	zookeeper_ip = ?
	zookeeper_port = ?
	*/
	
	m_config.LoadConfigFile(config_file.c_str());
#ifdef test

	std::cout<<"rpcserverip: "<<m_config.Load("rpcserverip")<<std::endl;
	std::cout<<"rpcserverport: "<<m_config.Load("rpcserverport")<<std::endl;
	std::cout<<"zookeeperip: "<<m_config.Load("zookeeperip")<<std::endl;
	std::cout<<"zookeeperport: "<<m_config.Load("zookeeperport")<<std::endl;
#endif
	
	 


	  

}

MprpcApplication& MprpcApplication::GetInstance(){
        static MprpcApplication app;
        return app;
}


MprpcConfig& MprpcApplication::GetConfig(){
	return m_config;
}